<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      {{#each columns}}
      {{#if isQuery}}
      {{#if (hbs_eq htmlType "input")}}
      <el-form-item label="{{columnComment}}" prop="{{tsField}}">
        <el-input
          v-model="queryParams.{{tsField}}"
          placeholder="请输入{{columnComment}}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      {{/if}}
      {{#if (hbs_and (hbs_or (hbs_eq htmlType "select") (hbs_eq htmlType "radio")) dictType)}}
      <el-form-item label="{{columnComment}}" prop="{{tsField}}">
        <el-select v-model="queryParams.{{tsField}}" placeholder="请选择{{columnComment}}" clearable>
          <el-option
            v-for="dict in dict.type.{{dictType}}"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      {{/if}}
      {{#if (hbs_and (hbs_eq htmlType "datetime") (hbs_not (hbs_eq queryType "BETWEEN")))}}
      <el-form-item label="{{columnComment}}" prop="{{tsField}}">
        <el-date-picker clearable
          v-model="queryParams.{{tsField}}"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择{{columnComment}}">
        </el-date-picker>
      </el-form-item>
      {{/if}}
      {{#if (hbs_and (hbs_eq htmlType "datetime") (hbs_eq queryType "BETWEEN"))}}
      <el-form-item label="{{columnComment}}">
        <el-date-picker
          v-model="daterange{{AttrName}}"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      {{/if}}
      {{/if}}
      {{/each}}
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['{{moduleName}}:{{businessName}}:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['{{moduleName}}:{{businessName}}:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['{{moduleName}}:{{businessName}}:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['{{moduleName}}:{{businessName}}:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="{{businessName}}List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      {{#each columns}}
      {{#if (hbs_eq isPk '1')}}
      <el-table-column label="{{columnComment}}" align="center" prop="{{tsField}}" />
      {{else if (hbs_and isList (hbs_eq htmlType "datetime"))}}
      <el-table-column label="{{columnComment}}" align="center" prop="{{tsField}}" width="180">
        <template slot-scope="scope">
          <span>\{{ parseTime(scope.row.{{tsField}}, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      {{else if (hbs_and isList (hbs_eq htmlType "imageUpload"))}}
      <el-table-column label="{{columnComment}}" align="center" prop="{{tsField}}" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.{{tsField}}" :width="50" :height="50"/>
        </template>
      </el-table-column>
      {{else if (hbs_and isList dictType)}}
<el-table-column label="{{columnComment}}" align="center" prop="{{tsField}}">
        <template slot-scope="scope">
          {{#if (hbs_eq htmlType "checkbox")}}
          <dict-tag :options="dict.type.{{dictType}}" :value="scope.row.{{tsField}} ? scope.row.{{tsField}}.split(',') : []"/>
          {{else}}
          <dict-tag :options="dict.type.{{dictType}}" :value="scope.row.{{tsField}}"/>
          {{/if}}
        </template>
      </el-table-column>
      {{else if (hbs_and isList tsField)}}
      <el-table-column label="{{columnComment}}" align="center" prop="{{tsField}}" />
      {{/if}}
      {{/each}}
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['{{moduleName}}:{{businessName}}:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['{{moduleName}}:{{businessName}}:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        {{#each columns}}
        {{#if isEdit}}
        {{#if (hbs_eq htmlType "input")}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-input v-model="form.{{tsField}}" placeholder="请输入{{columnComment}}" />
        </el-form-item>
        {{else if (hbs_eq htmlType "imageUpload")}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <image-upload v-model="form.{{tsField}}"/>
        </el-form-item>
        {{else if (hbs_eq htmlType "fileUpload")}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <file-upload v-model="form.{{tsField}}"/>
        </el-form-item>
        {{else if (hbs_eq htmlType "editor")}}
        <el-form-item label="{{columnComment}}">
          <editor v-model="form.{{tsField}}" :min-height="192"/>
        </el-form-item>
        {{else if (hbs_eq htmlType "select")}}
        {{#if dictType}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-select v-model="form.{{tsField}}" placeholder="请选择{{columnComment}}">
            <el-option
              v-for="dict in dict.type.{{dictType}}"
              :key="dict.value"
              :label="dict.label"
              {{#if (hbs_eq tsType "number")}} 
              :value="parseInt(dict.value)"
              {{else}}
              :value="dict.value"
              {{/if}}
            ></el-option>
          </el-select>
        </el-form-item>
        {{else}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-select v-model="form.{{tsField}}" placeholder="请选择{{columnComment}}">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        {{/if}}
        {{else if (hbs_eq htmlType "checkbox")}}
        {{#if dictType}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-checkbox-group v-model="form.{{tsField}}">
            <el-checkbox
              v-for="dict in dict.type.{{dictType}}"
              :key="dict.value"
              :label="dict.value">
              \{{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        {{else}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-checkbox-group v-model="form.{{tsField}}">
            <el-checkbox>请选择字典生成</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        {{/if}}
        {{else if (hbs_eq htmlType "radio")}}
        {{#if dictType}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-radio-group v-model="form.{{tsField}}">
            <el-radio
              v-for="dict in dict.type.{{dictType}}"
              :key="dict.value"
              {{#if (hbs_eq tsType "number")}}   
              :label="parseInt(dict.value)"
              {{else}}
              :label="dict.value"
              {{/if}}
            >\{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        {{else}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-radio-group v-model="form.{{tsField}}">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        {{/if}}
        {{else if (hbs_eq htmlType "datetime")}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-date-picker clearable
            v-model="form.{{tsField}}"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择{{columnComment}}">
          </el-date-picker>
        </el-form-item>
        {{else if (hbs_eq htmlType "textarea")}}
        <el-form-item label="{{columnComment}}" prop="{{tsField}}">
          <el-input v-model="form.{{tsField}}" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        {{/if}}
        {{/if}}
        {{/each}}
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list{{BusinessName}}, get{{BusinessName}}, del{{BusinessName}}, add{{BusinessName}}, update{{BusinessName}} } from "@/api/{{moduleName}}/{{businessName}}";

export default {
  name: "{{BusinessName}}",
  {{#if dicts}}
  dicts: [{{{dicts}}}],
  {{/if}}
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // {{functionName}}表格数据
      {{businessName}}List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        {{#each columns}}
        {{#if isQuery}}
        {{tsField}}: null{{#unless @last}},{{/unless}}
        {{/if}}
        {{/each}}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        {{#each columns}}
        {{#if required}}
        {{tsField}}: [
          { required: true, message: "{{columnComment}}不能为空", trigger: "blur" }
        ]{{#unless @last}},{{/unless}}
        {{/if}}
        {{/each}}
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list{{BusinessName}}(this.queryParams).then(response => {
        this.{{businessName}}List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        {{#each columns}}
        {{tsField}}: null{{#unless @last}},{{/unless}}
        {{/each}}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.{{pkColumn.tsField}});
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加{{functionName}}";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const {{pkColumn.tsField}} = row.{{pkColumn.tsField}} || this.ids;
      get{{BusinessName}}({{pkColumn.tsField}}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改{{functionName}}";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.{{pkColumn.tsField}} != null) {
            update{{BusinessName}}(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add{{BusinessName}}(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const {{pkColumn.tsField}}s = row.{{pkColumn.tsField}} || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + {{pkColumn.tsField}}s + '"的数据项？').then(function() {
        return del{{BusinessName}}({{pkColumn.tsField}}s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('{{moduleName}}/{{businessName}}/export', {
        ...this.queryParams
      }, `{{businessName}}_\${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
